package com.spark.market.dal.repository.impl;

import com.spark.market.common.utils.AssertUtils;
import com.spark.market.dal.dao.ItemStockDoMapper;
import com.spark.market.dal.entity.ItemStockDo;
import com.spark.market.dal.repository.ItemStockRepository;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class ItemStockRepositoryImpl implements ItemStockRepository {
    @Resource
    private ItemStockDoMapper itemStockDoMapper;
    @Override
    public void insertItemStock(ItemStockDo itemStockDo) {
        AssertUtils.isNotNull(itemStockDo,"itemStockDo is null");
        itemStockDoMapper.insertSelective(itemStockDo);
    }

    @Override
    public ItemStockDo selectItemStockById(Integer itemId) {
        AssertUtils.isNotNull(itemId, "itemId is null");
        return itemStockDoMapper.selectByItemId(itemId);
    }

    @Override
    public boolean decreaseStock(Integer itemId, Integer amount) {
        AssertUtils.isNotNull(itemId, "itemId is null");
        AssertUtils.isNotNull(amount, "amount is null");
        int affectedRow = itemStockDoMapper.updateStockAndItemIdByAmount(itemId, amount);
        if (affectedRow > 0) return true;
        return false;
    }
}
